﻿
var WindFunction = {
    InstallWindChart_History: function (chartobj, datas, intervalMinute, isreal) {
        var newData = [];
        fxNewData = [], pointStartMinutes = datas[0].data[0].time;
        for (var d = 0; d < datas[0].data.length; d++) {
            var tFs = datas[0].data[d].fs.toFixed(3) * 1;
            var tFx = datas[0].data[d].fx.toFixed(3) * 1;
            var tTime = datas[0].data[d].time * 1;

            newData.push([tFs, tFx]);
            fxNewData.push([tFs, tFx, tTime]); //风速，风向，时间戳
        };
        WindFunction.InstallWindChart(chartobj, newData, fxNewData, intervalMinute, isreal);
    },
    InstallWindChart: function (chartobj, datas, fxNewData, intervalMinute, isreal) {
        $(chartobj).empty();
        $(chartobj).highcharts({
            chart: {
                events: {
                    load: function () {
                        if (isreal) {
                            var uuid = $("#realwindow_title").data("uuid");
                            var nc = this;
                            var series = nc.series;
                            var windowRealDataSocket = window.WebSocket || window.MozWebSocket;
                            if (window.windowRealDataSocket !== undefined && window.windowRealDataSocket !== null) { window.windowRealDataSocket.close(); }
                            window.windowRealDataSocket = new windowRealDataSocket("wss://websocket.aeroiot.cn/Iot?uuid=" + uuid + "_" + 7);
                            window.windowRealDataSocket.onmessage = function(evt) {
                                if (evt.data) {
                                    var data = $.parseJSON(evt.data);
                                    var fx = data.FX * 1;
                                    var p = [data.FS.toFixed(2) * 1, fx];
                                    series[0].addPoint(p, true, false);//添加风向
                                    series[1].addPoint(p, true, false);//添加风速
                                }
                            };
                        }
                    }
                }
            },
            title: { text: null },
            xAxis: { type: 'datetime', offset: 0 },
            plotOptions: { series: { pointStart: pointStartMinutes, pointInterval: intervalMinute } },
            tooltip: {
                formatter: function () {
                    var nfx = 0;
                    for (var nf = 0; nf < fxNewData.length; nf++) {
                        if (fxNewData[nf][2] === this.key) {
                            nfx = fxNewData[nf][1] * 1;
                            break;
                        }
                    }
                    return '<span style="font-size: 10px;"> ' + (new Date(this.x)).format('MM-dd hh:mm') + '</span><br/>风速：<b>' + (this.y == undefined ? "--" : this.y) + ' m/s</b><br/>风向：<b>' + nfx + " °</b>";
                }
            },
            yAxis: { title: { text: '风速 m/s' } },
            series: [
                {
                    type: 'windbarb',
                    data: datas,
                    name: 'wind',
                    color: Highcharts.getOptions().colors[1],
                    showInLegend: false,
                    tooltip: { valueSuffix: ' m/s' }
                },
                {
                    type: 'area',
                    keys: ['y', 'rotation'],
                    data: datas,
                    color: Highcharts.getOptions().colors[0],
                    fillColor: { linearGradient: { x1: 0, x2: 0, y1: 0, y2: 1 }, stops: [[0, Highcharts.getOptions().colors[0]], [1, Highcharts.color(Highcharts.getOptions().colors[0]).setOpacity(0.25).get()]] },
                    showInLegend: false,
                    name: $("#realshowpoint").find("option:selected").text(),
                    tooltip: { valueSuffix: ' m/s' }
                }
            ]
        });
    },
    InstallWindChart_DoubleAxisData: function (chartobj, pointStartMinutes, historyNewData, historyFxNewData, intervalMinute) {
        $(chartobj).highcharts({
            title: { text: '风速风向历史数据分析' },
            xAxis: { type: 'datetime', offset: 0 },
            plotOptions: { series: { pointStart: pointStartMinutes * 1000, pointInterval: intervalMinute * 60 * 1000 } },
            tooltip: {
                formatter: function () {
                    var nfx = 0;
                    for (var nf = 0; nf < historyFxNewData.length; nf++) {
                        if (historyFxNewData[nf][2]+'000' === this.key+'') {
                            nfx = historyFxNewData[nf][1] * 1;
                            break;
                        }
                    }
                    return '<span style="font-size: 10px;">' + (new Date(this.x)).format('yyyy-MM-dd hh:mm:ss') + '</span><br/>风速：<b>' + this.y + ' m/s</b><br/>风向：<b>' + nfx + " °</b>";
                }
            },
            yAxis: {title: { text: '风速 m/s' }},
            series: [
                {
                    type: 'windbarb',
                    data: historyNewData,
                    name: 'wind',
                    color: Highcharts.getOptions().colors[1],
                    showInLegend: false,
                    tooltip: { valueSuffix: ' m/s' }
                },
                {
                    type: 'area',
                    keys: ['y', 'rotation'],
                    data: historyNewData,
                    color: Highcharts.getOptions().colors[0],
                    fillColor: { linearGradient: { x1: 0, x2: 0, y1: 0, y2: 1 }, stops: [[0, Highcharts.getOptions().colors[0]], [1, Highcharts.color(Highcharts.getOptions().colors[0]).setOpacity(0.25).get()]] },
                    name: $("#WindHistoryDataPointDetail").find("option:selected").text(),
                    tooltip: { valueSuffix: ' m/s' }
                }
            ]
        });
    }
}

function GetWindFxNameEn(value) {
    if (value === 0) { return "N"; }
    else if (value === 22.5) { return "NNE"; }
    else if (value === 45) { return "NE"; }
    else if (value === 67.5) { return "ENE"; }
    else if (value === 90) { return "E"; }
    else if (value === 112.5) { return "ESE"; }
    else if (value === 135) { return "SE"; }
    else if (value === 157.5) { return "SSE"; }
    else if (value === 180) { return "S"; }
    else if (value === 202.5) { return "SSW"; }
    else if (value === 225) { return "SW"; }
    else if (value === 247.5) { return "WSW"; }
    else if (value === 270) { return "W"; }
    else if (value === 292.5) { return "WNW"; }
    else if (value === 315) { return "NW"; }
    else if (value === 337.5) { return "NNW"; }
    return "ERROR";
}
function GetWindFxNameCn(value) {
    if (value === 0) { return "北"; }
    else if (value=== 22.5) { return "北北东"; }
    else if (value=== 45) { return "北东"; }
    else if (value=== 67.5) { return "东北东"; }
    else if (value=== 90) { return "东"; }
    else if (value=== 112.5) { return "东南东"; }
    else if (value=== 135) { return "南东"; }
    else if (value=== 157.5) { return "南南东"; }
    else if (value=== 180) { return "南"; }
    else if (value=== 202.5) { return "南南西"; }
    else if (value=== 225) { return "南西"; }
    else if (value=== 247.5) { return "西南西"; }
    else if (value=== 270) { return "西"; }
    else if (value=== 292.5) { return "西北西"; }
    else if (value=== 315) { return "北西"; }
    else if (value=== 337.5) { return "北北西";}
    return "ERROR";
}

function GetAllWindFxNameCns() {
    return ['北', '北北东', '北东', '东北东', '东', '东南东', '南东', '南南东', '南', '南南西', '南西', '西南西', '西', '西北西', '北西','北北西'];
}